//$Id: BaseServiceImpl Dec 27, 2009 3:17:28 PM encoding UTF-8 $ QQ:199742236 $ 
package com.hd.base.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.LockMode;
import org.hibernate.type.Type;
import org.springframework.dao.DataAccessException;

import cn.com.dstz.utils.DataBaseUtils;

import com.hd.base.dao.BaseDao;
import com.hd.base.service.BaseService;


public class BaseServiceImpl implements BaseService {

	private BaseDao baseDao;
	
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public Integer batchDelete(String HQL, Map<String, Object> valueMap)
			throws DataAccessException {
		return batchUpdate(HQL, valueMap);
	}

	public Integer batchUpdate(String HQL, Map<String, Object> valueMap)
			throws DataAccessException {
		return baseDao.batchUpdate(HQL, valueMap);
	}

	public Long count(String HQL, Map<String, Object> valueMap)
			throws DataAccessException {
		return get(HQL, valueMap , null);
	}

	public Long countByNamedQuery(String namedQuery, Object... values) {
		return getByNamedQuery(namedQuery, null, values);
	}

	public boolean delete(String entityName, Object entity)
			throws DataAccessException {
		boolean isDelete = false;
		try {
			baseDao.delete(entityName, entity);
			isDelete = true;
		}catch(DataAccessException e) {
			throw e;
		}
		return isDelete;
	}

	public boolean delete(Class clazz, Object entity)
			throws DataAccessException {
		
		return delete(clazz.getName() , entity);
	}

	public boolean delete(Object entity) throws DataAccessException {
		
		return delete(entity.getClass().getName() , entity);
	}

	public Integer delete(String HQL, Map<String, Object> valueMap)
			throws DataAccessException {

		return update(HQL , valueMap);
	}

	public Integer deleteByNamedQuery(String namedQuery, Object... values)
			throws DataAccessException {

		return baseDao.updateByNamedQuery(namedQuery, values);
	}

	public <T> List<T> find(String HQL, Map<String, Object> valueMap,
			int firstResult, int maxResult) throws DataAccessException {
		
		return baseDao.find(HQL, valueMap, firstResult, maxResult);
	}

	public <T> List<T> find(String HQL, Map<String, Object> valueMap)
			throws DataAccessException {
		
		return find(HQL , valueMap , null);
	}

	public <T> List<T> find(String HQL, Map<String, Object> valueMap,
			String cacheName) throws DataAccessException {

		return baseDao.find(HQL, valueMap, cacheName);
	}

	public List findAll(String HQL, Map<String, Object> valueMap)
			throws DataAccessException {

		return find(HQL, valueMap, 0, 0);
	}

	public <T> List<T> findAllByNamedQuery(String namedQuery, Object... values)
			throws DataAccessException {
		return baseDao.findByNamedQuery(namedQuery, null, 0, 0, values);
	}

	public <T> List<T> findAllByNamedQuery(String namedQuery, String cacheName,
			Object... values) throws DataAccessException {
		return baseDao.findByNamedQuery(namedQuery, cacheName, 0, 0, values);
	}

	public <T> List<T> findAllByNamedQuery(String namedQuery, String cacheName)
			throws DataAccessException {
		return baseDao.findByNamedQuery(namedQuery, cacheName, 0, 0, null , null);
	}

	public <T> List<T> findByNamedQuery(String namedQuery, String cacheName,
			int beginResult, int maxResult, Object... values)
			throws DataAccessException {
		return baseDao.findByNamedQuery(namedQuery, cacheName, beginResult, maxResult, values);
	}

	public <T> List<T> findByNamedQuery(String namedQuery, int beginResult,
			int maxResult, Object... values) throws DataAccessException {
		return baseDao.findByNamedQuery(namedQuery, null, beginResult, maxResult, values);
	}

	public <T> T get(String entityName, Serializable id)
			throws DataAccessException {
		return baseDao.get(entityName, id);
	}

	public <T> T get(Class<T> clazz, Serializable id)
			throws DataAccessException {
		return baseDao.get(clazz.getName(), id);
	}

	public <T> T get(String entityName, Serializable id, LockMode lockMode)
			throws DataAccessException {
		return baseDao.get(entityName, id, lockMode);
	}

	public <T> T get(Class<T> clazz, Serializable id, LockMode lockMode)
			throws DataAccessException {
		return get(clazz.getName(), id, lockMode);
	}

	public <T> T get(String HQL, Map<String, Object> valueMap)
			throws DataAccessException {
		return get(HQL, valueMap , null);
	}

	public <T> T get(String HQL, Map<String, Object> valueMap, String cacheName)
			throws DataAccessException {
		return baseDao.get(HQL, valueMap, cacheName);
	}

	public <T> T getByNamedQuery(String namedQuery, Object... values)
			throws DataAccessException {
		return baseDao.getByNamedQuery(namedQuery, null, values);
	}

	public <T> T getByNamedQuery(String namedQuery, String cacheName,
			Object... values) throws DataAccessException {
		return baseDao.getByNamedQuery(namedQuery, cacheName, values);
	}

	public <T> T load(String entityName, Serializable id)
			throws DataAccessException {
		return baseDao.load(entityName, id);
	}

	public <T> T load(Class<T> clazz, Serializable id) {
		return load(clazz.getName(), id);
	}

	public void load(Object entity, Serializable id) throws DataAccessException {
		baseDao.load(entity, id);	
	}

	public <T> T load(String entityName, Serializable id, LockMode lockMode)
			throws DataAccessException {
		return baseDao.load(entityName, id, lockMode);
	}

	public <T> T load(Class<T> clazz, Serializable id, LockMode lockMode)
			throws DataAccessException {
		return load(clazz.getName(), id, lockMode);
	}

	public boolean persist(String entityName, Object entity)
			throws DataAccessException {
		boolean isPersist = false ;
		try {
			baseDao.persist(entityName, entity);
		} catch (DataAccessException e) {
			throw e;
		}
		return isPersist;
	}

	public boolean persist(Object entity) throws DataAccessException {
		return persist(entity.getClass().getName(),entity);
	}

	public boolean persist(Class clazz, Object entity)
			throws DataAccessException {
		return persist(clazz.getName(), entity);
	}

	public boolean save(String entityName, Object entity)
			throws DataAccessException {
		boolean isSave = false;
		try {
			baseDao.save(entityName, entity);
			isSave = true;
		} catch (DataAccessException e) {
			throw e;
		}
		return isSave;
	}

	public boolean save(Object entity) throws DataAccessException {
		
		return save(entity.getClass().getName(), entity);
	}

	public boolean save(Class clazz, Object entity) throws DataAccessException {
		return save(clazz.getName(), entity);
	}

	public boolean saveOrUpdate(String entityName, Object entity)
			throws DataAccessException {
		boolean isSaveOrUpdate = false;
		try {
			baseDao.saveOrUpdate(entityName, entity);
			isSaveOrUpdate = true;
		} catch (DataAccessException e) {
			throw e;
		}
		return isSaveOrUpdate;
	}

	public boolean saveOrUpdate(Class clazz, Object entity)
			throws DataAccessException {
		return saveOrUpdate(clazz.getName(), entity);
	}

	public boolean saveOrUpdate(Object entity) throws DataAccessException {
		return saveOrUpdate(entity.getClass().getName(), entity);
	}

	public boolean update(String entityName, Object entity)
			throws DataAccessException {
		boolean isUpdate = false;
		
		try{
		    baseDao.update(entityName, entity);
		    isUpdate = true;
		}catch(DataAccessException e){
		    throw e;
		}
		
		return isUpdate;
	}

	public boolean update(Object entity) throws DataAccessException {
		return update(entity.getClass().getName(), entity);
	}

	public boolean update(Class clazz, Object entity)
			throws DataAccessException {
		return update(clazz.getName(), entity);
	}

	public Integer update(String HQL, Map<String, Object> valueMap)
			throws DataAccessException {
		return baseDao.update(HQL, valueMap);
	}

	public Integer updateByNamedQuery(String namedQuery, Object... values)
			throws DataAccessException {
		return baseDao.updateByNamedQuery(namedQuery, values);
	}

	public Integer batchDeleteByNamedQuery(String namedQuery, Object... values)
			throws DataAccessException {
		return baseDao.updateByNamedQuery(namedQuery, values);
	}

	public Integer batchUpdateByNamedQuery(String namedQuery, Object... values)
			throws DataAccessException {
		return baseDao.updateByNamedQuery(namedQuery, values);
	}
	//通过sql语句查询记录条数；
	public int getListCountBySql(String sql){
		return baseDao.getListCountBySql(sql);
	}
	//通过sql语句查询记录；
	public List getListBySql(String sql, int currentPage, int pageNums){
		return baseDao.getListBySql(sql, currentPage, pageNums);
	}
	
	
	// ==========================================================================
	
	public List< Map< String, Object >> queryList( String sql, int beginSize, int pageSize, Map< String, Object  > valueMap ){
		List< Map< String, Object >> list = null;
		
		try{
			list = baseDao.findListBySQLQuery( sql, beginSize, pageSize, valueMap );
		}catch( Exception e ){
			e.printStackTrace();
		}
		return list;
	}
	
	public int countBySQL( String sql, Map< String, Object > valueMap ){
		int count = 0;
		try{
			count = baseDao.count( sql, valueMap );
		}catch( Exception e ){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Map< String, Object > queryForMap( String sql, Map< String, Object > valueMap ){
		Map< String, Object > data = null;
		try{
			data = baseDao.get( sql, valueMap );
		}catch( Exception e ){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}
	
	public List< Map< String, Object >> queryForListByAll( String sql, Map< String, Object > valueMap ){
		List< Map< String, Object >> list = null;
		try{
			list = baseDao.findAllListBySQLQuery( sql, valueMap );
		}catch( Exception e ){
			e.printStackTrace();
		}
		return list;
	}
	
	public int save ( String tableName, Map < String, Object > insertData ) {
		return save( tableName, insertData, false );
	}
	
	public int deleteBySql( String tableName, Map< String, Object > whereData ){
		
		String comma = "";
		StringBuffer whereSQL = new StringBuffer ( );
		if ( whereData == null || whereData.isEmpty ( ) ) {
			whereSQL.append ( "1" );
		} else {
			for ( Entry < String, Object > entry : whereData.entrySet ( ) ) {
				whereSQL.append ( comma + "`" + entry.getKey ( ) + "`='" + entry.getValue ( ) + "'" );
				comma = " AND ";
			}
		}
		
		int del = 0;
		
		try{
			del = baseDao.saveOrUpdate ( "delete from "	+  tableName +  " where " + whereSQL );
		}catch( Exception e ){
			e.printStackTrace();
		}
		
		return del;
	}
	
	public int update( String tableName, Map<String, Object > updateData, Map< String, Object > whereData ){
		
		String comma = "";
		StringBuffer setSQL = new StringBuffer ( );
		for ( Entry < String, Object > entry : updateData.entrySet ( ) ) {
			setSQL.append ( comma + "`" + entry.getKey ( ) + "`='"
					+ entry.getValue ( ) + "'" );
			comma = ", ";
		}
		comma = "";
		StringBuffer whereSQL = new StringBuffer ( );
		if ( whereData == null || whereData.isEmpty ( ) ) {
			whereSQL.append ( "1" );
		} else {
			for ( Entry < String, Object > entry : whereData.entrySet ( ) ) {
				whereSQL.append ( comma + "`" + entry.getKey ( ) + "`='" + entry.getValue ( ) + "'" );
				comma = " AND ";
			}
		}
		
		int update = 0;
		
		try{
			update = baseDao.saveOrUpdate ( "update "	+  tableName + " set " + setSQL + " where " + whereSQL );
		}catch( Exception e ){
			e.printStackTrace();
		}
		
		return update;
	}
	
	public int saveOreplace( String tableName, Map< String, Object > replaceData ){
		String sql = DataBaseUtils.getReplaceSQL( tableName, replaceData );
		
		int replace = 0;
		
		try{
			replace = baseDao.saveOrUpdate( sql );
		}catch( Exception e ){
			e.printStackTrace();
		}
		
		return replace;
	}
	 
	public int save( String tableName, Map< String, Object > insertData, boolean returnId ){
		
		String sql = DataBaseUtils.getInsertSQL( tableName, insertData );
		
		if( returnId ){
			try{
				return baseDao.saveAndReturnLastInsert( sql );
			}catch( Exception e ){
				e.printStackTrace();
			}
		}
		
		int save = 0;
		try{
			save = baseDao.saveOrUpdate( sql );
		}catch( Exception e ){
			e.printStackTrace();
		}
		return save;
	}
	
	public int saveOrUpdateBatch( List< String > sqls ){
		int batch = 0;
		try{
			batch = baseDao.batchSaveOrUpdate( sqls );
		}catch( Exception e ){
			e.printStackTrace();
		}
		return batch;
	}
	
	public Map< String, Object > getData( String sql ){
		Map< String, Object > data = null;
		try{
			data = baseDao.getData( sql );
		}catch( Exception e ){
			e.printStackTrace();
		}
		return data;
	}
	
	public List< Map< String, Object >> queryList( String sql, int beginSize, int pageSize, Map< String, Object> valueMap, Map< String, Type > aliasMap ){
		List< Map< String, Object >> list = null;
		try{
			list = baseDao.findListBySQLQuery( sql, beginSize, pageSize, valueMap, aliasMap );
		}catch( Exception e ){
			e.printStackTrace();
		}
		return list;
	}
	
}
